﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUL;
using Public;

namespace Presentation
{
    public partial class frmCreatePName : Form
    {
        public frmCreatePName()
        {
            InitializeComponent();
        }
        ProductNameBUL proName = new ProductNameBUL();
        int selectedPNameId;

        private void frmCreatePName_Load(object sender, EventArgs e)
        {
            searchProName("");
            LoadProductTypeCBX();
        }

        public void LoadProductTypeCBX()
        {
            ProductTypeBUL productType = new ProductTypeBUL();
            cbxProType.DataSource = productType.GetAllProductTypeCBX().Tables[0];
            cbxProType.DisplayMember = "typeName";
            cbxProType.ValueMember = "typeId";
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string searchValue = txtSearch.Text.Trim();
            searchProName(searchValue);
        }

        public void searchProName(string searchValue)
        {
            DataTable dt = proName.searchProName(searchValue);
            //ProductName pn = new ProductName();
            dgvProName.DataSource = proName.searchProName(searchValue);
            dgvProName.Columns[2].Visible = false;
            //List<object> lst = dt.AsEnumerable().ToList<object>();
            //List<object> lst = dt.AsEnumerable().ToList<object>();
            //List<ProductName> lst = dt.AsEnumerable().ToList<ProductName>();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ProductName pn = new ProductName();
            pn.PName = txtProName.Text.Trim();
            string selectedValue = cbxProType.SelectedValue.ToString();
            pn.TypeId = int.Parse(selectedValue);
            proName.AddProductName(pn);
            frmCreatePName_Load(sender, e);
        }

        private void AddPType_Click(object sender, EventArgs e)
        {
            frmCreatePType frmCreateType = new frmCreatePType();
            frmCreateType.Show();
            this.Hide();
        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            ProductName pn = new ProductName();
            pn.PName = txtProName.Text.Trim();
            string selectedValue = cbxProType.SelectedValue.ToString();
            pn.TypeId = int.Parse(selectedValue);
            pn.PNameId = this.selectedPNameId;
            proName.UpdateProductName(pn);
            frmCreatePName_Load(sender, e);
        }

        private void btnAddProType_Click(object sender, EventArgs e)
        {
            frmCreatePType frmCreateType = new frmCreatePType();
            frmCreateType.Show();
            this.Hide();
        }

        private void selectedItem(object sender, MouseEventArgs e)
        {
            int index = dgvProName.CurrentRow.Index;
            this.txtProName.Text = dgvProName.Rows[index].Cells[0].Value.ToString();
            string typeName = dgvProName.Rows[index].Cells[1].Value.ToString();
            cbxProType.SelectedIndex = cbxProType.FindStringExact(typeName);
            string proNameId = dgvProName.Rows[index].Cells[2].Value.ToString();
            this.selectedPNameId = int.Parse(proNameId);
            //this.cbxProType.DisplayMember = dgvProName.Rows[index].Cells[1].Value.ToString();
        }

        private void frmPName_Closing(object sender, FormClosingEventArgs e)
        {
            DialogResult rs = MessageBox.Show("Are you really want to exist?", "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (rs == DialogResult.OK)
            {
                frmManageBatch manageBatch = new frmManageBatch();
                this.Hide();
                manageBatch.Show();
            }
            else
            {
                e.Cancel = true;
            }
        }
    }
}
